package company.zuiyou;

public class Z5 {
    //1.暴力解
    public int maxArea(int[] nums) {
        int length = nums.length;
        int maxArea = 0;
        int minHeight = Integer.MAX_VALUE;
        for (int i = 0; i < length; i++) {
            minHeight = nums[i];
            for (int j = i + 1; j < length; j++) {
                if (nums[j] < minHeight) {
                    minHeight = nums[j];
                }
                  maxArea = Math.max(maxArea, (j - i) * minHeight);
            }
        }
        return maxArea;
    }

    //2.类似股票
    // dp[i] = Math.max(dp[i-1]+1*num[i]);
    // height更新
    // 1 2 3 4 3 2 1
    public int maxArea1(int[] nums) {
        int length = nums.length;
        int maxArea = 0;
        int height;
        for (int i = 0; i < length; i++) {
            height = nums[i];
            int j = i;
            while (j > length && nums[j] >= height) {
                j++;
            }
            maxArea = Math.max(maxArea, (j - i) * height);
        }
        return maxArea;
    }

    // 1*1 1 1*2 (1+1)*2
    public int maxArea2(int[] nums) {
        int length = nums.length;
        int[] dp = new int[length];
        dp[0] = nums[0];
        int lastHeight = nums[0];
        int lastWeight = 1;
        for (int i = 1; i < length; i++) {
            if (nums[i] > lastHeight * (lastWeight + 1)) {
                dp[i] = nums[i];
                lastHeight = nums[i];
                lastWeight = 1;
            } else {
                dp[i] = lastHeight * (lastWeight + 1);
                lastWeight++;
            }
        }
        return dp[length - 1];
    }

}
